﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TryCancer
{
    class SystemeNeuronne
    {
        neurone[] caches;
        neurone sortie;
        const int nb_entree = 3;
        int nb_cache;
        public SystemeNeuronne(int _nb_cache)
        {
            nb_cache = _nb_cache;
            caches = new neurone[nb_cache];
            for (int i = 0; i < nb_cache; i++)
            {
                caches[i] = new neurone(3);
            }
            this.sortie = new neurone(_nb_cache);
        }

        public double evaluate(double[] donnee)
        {
            foreach (neurone neu in caches)
            {
                neu.Clear();
            }
            sortie.Clear();
            double[] sortieCache = new double[nb_cache];
            for (int i = 0; i < nb_cache; i++)
            {
                sortieCache[i] = caches[i].evaluate(donnee);
            }
            double outputs;
            //Console.WriteLine(" POIDS = D1: " + sortie.poids[0] + " |D2: " + sortie.poids[1] + " |D3:" + sortie.poids[2]);
            outputs = sortie.evaluate(sortieCache);
            return outputs;
        }

        public void poidsControle(double[] donnee, double rate)
        {

            double valeurSorti = sortie.output;
            double sortiAttend = donnee[3];
            double deltaSorti = valeurSorti * (1 - valeurSorti) * (sortiAttend - valeurSorti);//bon
            double[] deltaCache = new double[nb_cache];
            for (int i = 0; i < nb_cache; i++)
            {
                double somme = 0.0;

                somme = deltaSorti * sortie.poids[i];
                deltaCache[i] = caches[i].output * (1 - caches[i].output) * somme;//bon
            }
            double value;
            for (int j = 0; j < nb_cache; j++)
            {
                value = sortie.poids[j] + rate * deltaSorti * caches[j].output;//bon
                sortie.poids[j] = value;
            }
            value = sortie.poids[nb_cache] + rate * deltaSorti * 1.0;
            sortie.poids[nb_cache] = value;
            double[] Entre = new double[3];
            Entre[0] = donnee[0];
            Entre[1] = donnee[1];
            Entre[2] = donnee[2];
            for (int k = 0; k < nb_cache; k++)
            {
                for (int l = 0; l < nb_entree; l++)
                {
                    value = caches[k].poids[l] + rate * deltaCache[k] * Entre[l];//bon
                    caches[k].poids[l] = value;
                }
                value = caches[k].poids[nb_entree] + rate * deltaCache[k] * 1.0;//bon
                caches[k].poids[nb_entree] = value;
            }
        }
        public double[] getPoids()
        {
            double[] result = new double[3];
            result[0]= sortie.poids[0];
            result[1]= sortie.poids[1];
            result[2] = sortie.poids[2];
            return result;
        }
    }
}
